package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.BaseEntity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity.EntryLevel2Entity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity.EntryLevel2Entity.EntryLevel3Entity;
import com.hctforgreen.greenservice.utils.EntryDetailUtil;
import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 资料目录列表适配器
 * 
 * @author Tallen Zhai
 * 
 */
public class EntryCatalogListAdapter extends BaseAdapter implements
		OnItemClickListener, OnScrollListener {

	private Activity mActivity;
	private View mConvertView;
	private ListView mListView;

	private static List<EntryLevel1Entity> mEntities;
	private String mMachineName;
	private boolean bUseCurrentId;
	private EntryCatalogEntity catalogEntity;

	public EntryCatalogListAdapter(ListView listView, Activity activity,
			View convertView, EntryCatalogEntity catalogEntity,
			List<EntryLevel1Entity> entities, String machineName,
			boolean bUseCurrentId) {

		mListView = listView;
		mActivity = activity;
		mConvertView = convertView;
		this.catalogEntity = catalogEntity;
		mEntities = entities;
		mMachineName = machineName;
		this.bUseCurrentId = bUseCurrentId;
		mListView.setAdapter(this);

		mListView.setOnScrollListener(this);
		mListView.setOnItemClickListener(this);

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final EntryLevel1Entity entity = (EntryLevel1Entity) getItem(position);
		// if (convertView == null) {
		convertView = View.inflate(mActivity, R.layout.item_entry_catalog_lst,
				null);
		viewCache = new ViewCache(convertView);
		convertView.setTag(viewCache);
		// } else {
		// viewCache = (ViewCache) convertView.getTag();
		// }
		viewCache.getTitleContentsTv().setText(entity.titleContents);

		if (entity.isOff) {
			viewCache.getLevel1FooterLyt().setVisibility(View.GONE);
			viewCache.getSwitchTv().setBackgroundResource(R.drawable.ic_off);
		} else if (!entity.isOff) {
			viewCache.getLevel1FooterLyt().setVisibility(View.VISIBLE);
			viewCache.getSwitchTv().setBackgroundResource(R.drawable.ic_on);
		}

		viewCache.getLevel1Lyt().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (entity.dataList == null) {
					HctConstants.level1Entity = entity;
					EntryDetailUtil detailUtil = new EntryDetailUtil();
					detailUtil.startEntryDetailActivity(mActivity,
							catalogEntity, bUseCurrentId, mMachineName,
							entity.id, entity.titleContents, entity.bookId, 1);
					return;
				}
				if (entity.isOff) {
					for (BaseEntity tmpEntity : mEntities) {
						((EntryLevel1Entity) tmpEntity).isOff = true;
					}
					entity.isOff = false;
					notifyDataSetChanged();
					if (!entity.isOff) {
						mListView.setSelection(position);
					}
				} else if (!entity.isOff) {
					entity.isOff = !entity.isOff;
					notifyDataSetChanged();
				}

			}
		});
		if (entity.dataList == null) {
			viewCache.getSwitchTv().setVisibility(View.GONE);
		}
		initLevel2Layout(viewCache.getLevel1FooterLyt(), entity);
		return convertView;
	}

	private void initLevel2Layout(LinearLayout Level1FooterLyt,
			EntryLevel1Entity level1Entity) {

		if (level1Entity.dataList == null) {
			return;
		}

		for (int i = 0; i < level1Entity.dataList.size(); i++) {
			final LinearLayout level2Lyt = (LinearLayout) mActivity
					.getLayoutInflater().inflate(
							R.layout.item_entry_catalog_level_2_lst, null);

			final EntryLevel2Entity entity = level1Entity.dataList.get(i);
			level2Lyt.setId(i);
			TextView titleContentsTv = (TextView) level2Lyt
					.findViewById(R.id.tv_title_contents);
			titleContentsTv.setText(entity.titleContents);

			level2Lyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					//
					HctConstants.level2Entity = entity;
					EntryDetailUtil detailUtil = new EntryDetailUtil();
					detailUtil.startEntryDetailActivity(mActivity,
							catalogEntity, bUseCurrentId, mMachineName,
							entity.id, entity.titleContents, entity.bookId, 2);
				}
			});
			initLevel3Layout(level2Lyt, entity);
			Level1FooterLyt.addView(level2Lyt, i);
		}

	}

	private void initLevel3Layout(LinearLayout level2Lyt,
			final EntryLevel2Entity level2Entity) {

		if (level2Entity.dataList == null) {
			return;
		}

		for (int i = 0; i < level2Entity.dataList.size(); i++) {
			final LinearLayout level3Lyt = (LinearLayout) mActivity
					.getLayoutInflater().inflate(
							R.layout.item_entry_catalog_level_3_lst, null);

			final EntryLevel3Entity entity = level2Entity.dataList.get(i);
			level3Lyt.setId(i);
			TextView titleContentsTv = (TextView) level3Lyt
					.findViewById(R.id.tv_title_contents);
			titleContentsTv.setText(entity.titleContents);
			LinearLayout level2FooterLyt = (LinearLayout) level2Lyt
					.findViewById(R.id.lyt_level_2_footer);
			level3Lyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					HctConstants.level2Entity = level2Entity;
					EntryDetailUtil detailUtil = new EntryDetailUtil();
					detailUtil.startEntryDetailActivity(mActivity,
							catalogEntity, bUseCurrentId, mMachineName,
							entity.id, entity.titleContents, entity.bookId, 3);
				}
			});
			level2FooterLyt.addView(level3Lyt, i);
		}

	}

	class ViewCache {
		private View baseView;
		private FrameLayout level1Lyt;
		private LinearLayout level1FooterLyt;
		private TextView titleContentsTv;
		private ImageView switchIv;

		public ViewCache(View view) {
			baseView = view;
		}

		public FrameLayout getLevel1Lyt() {
			if (level1Lyt == null) {
				level1Lyt = (FrameLayout) baseView
						.findViewById(R.id.lyt_level_1);
			}
			return level1Lyt;
		}

		public LinearLayout getLevel1FooterLyt() {
			if (level1FooterLyt == null) {
				level1FooterLyt = (LinearLayout) baseView
						.findViewById(R.id.lyt_level_1_footer);
			}
			return level1FooterLyt;
		}

		public TextView getTitleContentsTv() {
			if (titleContentsTv == null) {
				titleContentsTv = (TextView) baseView
						.findViewById(R.id.tv_title_contents);
			}
			return titleContentsTv;
		}

		public ImageView getSwitchTv() {
			if (switchIv == null) {
				switchIv = (ImageView) baseView.findViewById(R.id.iv_switch);
			}
			return switchIv;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	}

	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if ((firstVisibleItem + visibleItemCount) == totalItemCount) {

		}

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}
}
